package edu.uta.cse6339.facetedinterface.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class ProcessRange {

	
	public static HashMap<Integer,Set<String>> readRangeFile(String fileName ){
		HashMap<Integer,Set<String>> rangeMap=new HashMap<Integer,Set<String>>();
		String line=null;
		File file = new File(fileName);
		BufferedReader reader;
		try {
				reader = new BufferedReader(new FileReader(file));
				while((line=reader.readLine())!=null && line.length()>1){
					String keyValue[]=line.split(":");
					if(keyValue!=null && keyValue.length>0){
						//System.out.println("Key : "+keyValue[0]);
						//System.out.println("Values : "+keyValue[1]);
						String values[]=keyValue[1].split(",");
						Set<String> valuesSet = new HashSet<String>();
						for(String value:values){
							valuesSet.add(value.trim().toLowerCase());
						}
						int key = Integer.valueOf(keyValue[0].trim());
						rangeMap.put(key, valuesSet);
					
					}
				}
				System.out.println("Range creating succesfull !");
			} catch (FileNotFoundException e) {
			System.out.println("Range file doesn't exist, check fileName"+fileName);
			e.printStackTrace();
			} catch (IOException e) {
			System.out.println("Exception while reading the content of the file "+ fileName);
			e.printStackTrace();
		}	
		return rangeMap;
	}
	
	public static Integer[]  sortRangeKeyValues(HashMap<Integer,Set<String>> rangeMap){
		Integer rangeKey []=null;
		if(rangeMap!=null && rangeMap.size()>0){
			Set<Integer> keySet =rangeMap.keySet();
			rangeKey= new Integer[keySet.size()];
			keySet.toArray(rangeKey);
			Arrays.sort(rangeKey);
			System.out.println("Sorted range file !");
		}
		else
		{
			System.out.println("stateRange map is null or empty");
		}
		return rangeKey;
	}
	

}
